package com.yunsoft.service.impl;

import java.util.List;


import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yunsoft.dao.StudentMapper;
import com.yunsoft.pojo.Student;
import com.yunsoft.service.StudentService;
@Service(value="IStudentService")
public class StudentServiceImpl extends BaseServiceImpl implements StudentService {

	@Override
	public void create(String name, Integer age,Integer Marks,Integer Year) {
		// TODO Auto-generated method stub
		try {
			String insertSql = "insert into Student(name,age) values(?,?)";
			jdbcTemplate.update(insertSql, name,age);
			String qury = "select max(id) from Student";
			int sid = jdbcTemplate.queryForInt(qury);
			String insertMarksSql = "insert into Marks(sid,marks,year) values(?,?,?)";
			jdbcTemplate.update(insertMarksSql,new Object[]{sid,Marks,Year});
			System.out.println("Created Name = " + name + ", Age = " + age);
			throw new RuntimeException("simulate Error condition") ;
		} catch (DataAccessException e) {
			// TODO: handle exception
			System.out.println("this commit have thorw error");
			throw e;
		}
	}

	@Override
	public Student getStudent(Integer id) {
		String sql = "select * from Student where id =?";
		Student stud =jdbcTemplate.queryForObject(sql, new Object[]{id}, new StudentMapper());
		return stud;
	}

	@Override
	public List<Student> getStudents() {
		List<Student> students = this.jdbcTemplate.query("select * from Student", new StudentMapper());
		return students;
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		this.jdbcTemplate.update("delete from Student where id =?", new Object[]{id});
		return ;
	}

	@Override
	public void update(Integer id, Integer age) {
		// TODO Auto-generated method stub
		this.jdbcTemplate.update("update Student set age=? where id = ?",new Object[]{age,id});
		return ;
	}


	@Override
	public void insertStudent(String name, Integer age) {
		// TODO Auto-generated method stub
		String sql = "insert into Student(name,age) values(?,?)";
		jdbcTemplate.update(sql, new Object[]{name,age});
	}
}
